package top.jybill.utils;

import java.sql.Connection;
import java.sql.SQLException;

public class ThreadManager implements AutoCloseable {

  private ConnectionUtils connectionUtils;

  public void setConnectionUtils(ConnectionUtils connectionUtils) {
    this.connectionUtils = connectionUtils;
  }

  /**
   * 获取连接
   * @return
   */
  public Connection getConnection() {
    return connectionUtils.getThreadConnection();
  }

  /**
   * 开始事务
   */
  public void begainTransfer() {
    try {
      connectionUtils.getThreadConnection().setAutoCommit(false);
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  /**
   * 回滚事务
   */
  public void rollback() {
    try {
      connectionUtils.getThreadConnection().rollback();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 提交
   */
  public void commit() {
    try {
      connectionUtils.getThreadConnection().commit();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 自动关闭 - tr-with-resource 释放连接
   */
  public void close() {
    connectionUtils.release();
  }


}
